Page({
  data: {
    boundCameras: [
      {
        id: 1,
        name: '厨房摄像头',
        online: true
      },
      {
        id: 2,
        name: '冰箱摄像头',
        online: true
      }
    ],
    showInputPopup: false,
    deviceCode: ''
  },

  onLoad: function() {
    // 页面加载时的逻辑
    this.initCamera();
  },
  
  onShow: function() {
    // 页面显示时的逻辑
    if (typeof this.getTabBar === 'function' && this.getTabBar()) {
      this.getTabBar().setData({
        active: 2
      });
    }
  },

  // 初始化摄像头
  initCamera: function() {
    wx.getSetting({
      success: (res) => {
        if (!res.authSetting['scope.camera']) {
          wx.authorize({
            scope: 'scope.camera',
            success: () => {
              // 授权成功
            },
            fail: () => {
              wx.showToast({
                title: '请授权使用摄像头',
                icon: 'none'
              });
            }
          });
        }
      }
    });
  },

  // 相机错误处理
  cameraError: function(e) {
    console.error('相机错误：', e);
    wx.showToast({
      title: '相机启动失败',
      icon: 'none'
    });
  },

  // 显示手动输入框
  showManualInput: function() {
    this.setData({
      showInputPopup: true
    });
  },

  // 关闭弹窗
  onPopupChange: function(e) {
    if (!e.detail.visible) {
      this.setData({
        showInputPopup: false
      });
    }
  },

  // 输入设备码变更
  onDeviceCodeChange: function(e) {
    this.setData({
      deviceCode: e.detail.value
    });
  },

  // 取消输入
  cancelInput: function() {
    this.setData({
      showInputPopup: false,
      deviceCode: ''
    });
  },

  // 确认输入设备码
  confirmInput: function() {
    if (!this.data.deviceCode) {
      wx.showToast({
        title: '请输入设备码',
        icon: 'none'
      });
      return;
    }

    wx.showLoading({
      title: '正在绑定',
      mask: true
    });

    // 模拟绑定过程
    setTimeout(() => {
      // 添加新绑定的摄像头
      const newCamera = {
        id: Date.now(),
        name: `摄像头 ${this.data.boundCameras.length + 1}`,
        online: true
      };

      this.setData({
        boundCameras: [...this.data.boundCameras, newCamera],
        showInputPopup: false,
        deviceCode: ''
      });

      wx.hideLoading();
      wx.showToast({
        title: '绑定成功',
        icon: 'success'
      });
    }, 1500);
  },

  // 显示摄像头选项
  showCameraOptions: function(e) {
    const id = e.currentTarget.dataset.id;
    wx.showActionSheet({
      itemList: ['重命名', '查看直播', '解除绑定'],
      success: (res) => {
        if (res.tapIndex === 2) {
          // 解除绑定
          this.unbindCamera(id);
        } else {
          // 其他功能暂未实现
          wx.showToast({
            title: '功能开发中',
            icon: 'none'
          });
        }
      }
    });
  },

  // 解除绑定
  unbindCamera: function(id) {
    wx.showModal({
      title: '解除绑定',
      content: '确定要解除此摄像头的绑定吗？',
      success: (res) => {
        if (res.confirm) {
          const boundCameras = this.data.boundCameras.filter(camera => camera.id !== id);
          this.setData({
            boundCameras
          });
          wx.showToast({
            title: '已解除绑定',
            icon: 'success'
          });
        }
      }
    });
  }
}); 